package org.bp.spider.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author baiping
 * @version 1.0
 * @date 2017/9/6
 */
public class FileUtils {
    public static boolean appendLineToFile(File file, String str) {
        FileWriter writer = null;

        boolean e1;
        try {
            writer = new FileWriter(file, true);
            writer.write(str + System.lineSeparator());
            boolean e = true;
            return e;
        } catch (Exception var14) {
            var14.printStackTrace();
            e1 = false;
        } finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            } catch (Exception var13) {
                var13.printStackTrace();
            }

        }
        return e1;
    }


    public static boolean writeToFile(File file, String str, String encoding) {
        BufferedWriter writer = null;

        boolean e1;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
            writer.write(str);
            boolean e = true;
            return e;
        } catch (Exception var15) {
            var15.printStackTrace();
            e1 = false;
        } finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            } catch (Exception var14) {
                var14.printStackTrace();
            }

        }
        return e1;
    }

    @SuppressWarnings("unchecked")
	public static List<String> loadTextInLineFromFile(File path, String encoding) {
        FileInputStream fileInputStream = null;

        Object e1;
        try {
            fileInputStream = new FileInputStream(path);
            List<String> e = loadTextInLineFromStream(fileInputStream, encoding);
            return e;
        } catch (Exception var14) {
            e1 = null;
            var14.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (Exception var13) {
                    var13.printStackTrace();
                }
            }

        }
        return (List<String>) e1;
    }

    public static List<String> loadTextInLineFromStream(InputStream stream, String encoding) {
        try {
            BufferedReader e = new BufferedReader(new InputStreamReader(stream, encoding));

            try {
                ArrayList<String> lst = new ArrayList<>();

                String line;
                while ((line = e.readLine()) != null) {
                    lst.add(line);
                }

                ArrayList<String> var5 = lst;
                return var5;
            } finally {
                e.close();
            }
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }
    public static String loadTextFile(File path, String encoding) {
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(path);
            String e = loadTextFromStream(fileInputStream, encoding);
            return e;
        } catch (Exception var14) {
            var14.printStackTrace();
        } finally {
            if(fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (Exception var13) {
                    var13.printStackTrace();
                }
            }

        }

        return null;
    }

    public static String loadTextFromStream(InputStream stream, String encoding) {
        List<String> lines = loadTextInLineFromStream(stream, encoding);
        if(lines == null) {
            return null;
        } else {
            StringBuilder str = new StringBuilder();
            Iterator<String> var4 = lines.iterator();

            while(var4.hasNext()) {
                String line = (String)var4.next();
                str.append(line).append("\n");
            }

            return str.toString();
        }
    }

    public static boolean copyFile(String srcPath, String destPath) {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        createFolders(destFile.getParent());
        FileInputStream is = null;
        FileOutputStream os = null;

        boolean count;
        try {
            is = new FileInputStream(srcFile);
            os = new FileOutputStream(destFile);
            byte[] e = new byte[1024];
            count = false;

            int count1;
            while((count1 = is.read(e)) != -1) {
                os.write(e, 0, count1);
            }

            boolean e1 = true;
            return e1;
        } catch (FileNotFoundException var25) {
            var25.printStackTrace();
            count = false;
            return count;
        } catch (Exception var26) {
            count = false;
        } finally {
            StreamUtils.close(os,is);
        }

        return count;
    }
    public static boolean createFolders(String folderPath) {
        File myFilePath = new File(folderPath);

        try {
            return !myFilePath.exists()?myFilePath.mkdirs():true;
        } catch (Exception var4) {
            var4.printStackTrace();
            return false;
        }
    }


}
